package com.aaa.web.service.impl.repairserviceimpl;

import com.aaa.entity.AppUserinfo;
import com.aaa.entity.BackEmployeeinfo;
import com.aaa.entity.BackExpenseorder;
import com.aaa.entity.BackExpenserecord;
import com.aaa.entity.repair.*;
import com.aaa.util.OrderNum;
import com.aaa.util.RandomUtil;
import com.aaa.web.commons.EmpQuery;
import com.aaa.web.commons.Result;
import com.aaa.web.commons.WlCommonsCode;
import com.aaa.web.dao.irepairdao.IRepairDao;
import com.aaa.web.service.irepairservice.IRepairService;
import com.aaa.web.systemconst.ISystemConst;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @description: 维修信息
 * @author: lianghuige
 * @create: 2018-12-25 13:14
 **/
@Service
public class RepairServiceImpl implements IRepairService {
    @Autowired
    private IRepairDao repairDao;

    /**
     * 获取门店id
     * 根据门店id获取该门店的维修信息
     * @return
     */
    @Override
    public Map findRepairInfo(EmpQuery query, BackCarRepair backCarRepair) {
        //分页
        int page = query.getPage();
        int size = query.getLimit();
        PageHelper.startPage(page,size);
        Map m = new HashMap();
        //获取门店id
        int schoolId= WlCommonsCode.isAdmin();
        backCarRepair.setSchoolId(schoolId);

        m.put("data",repairDao.findRepairInfo(backCarRepair));
        m.put("count",repairDao.getRepairInfoCount());
        m.put("code", ISystemConst.SU_COD);
        m.put("msg","");
        return m;
    }

    /**
     * 根据repairid删除对应的维修单信息----维修单展示表
     * @param repairid
     * @return
     */
    @Transactional
    @Override
    public Result delRepairInfoById(String repairid) {
        //维修单中的零件订单的删除
        repairDao.delExpenseOrderById(repairid);
        //仓库中对应零件的返回--仓库零件+1
        //根据repairid获取该订单的商品id
        List<BackExpenseorder> goodsIds = repairDao.findGoodsById(repairid);

        for (int i=0;i<goodsIds.size();i++){
            //遍历获取goodsid ---有维修费用的话查询的为null,所以需要判断非null否则报错
            if(goodsIds.get(i)!=null){
                int goodsid = (int) goodsIds.get(i).getGoodsid();
                //仓库零件+1
                repairDao.updateEntrepotNum(goodsid);
            }
        }
        //维修单的删除

        int ret = repairDao.delRepairInfoById(repairid);
        Result result = new Result();
            if(ret>ISystemConst.SU_COD){
                result.setMsg("删除成功！");
            }else{
                result.setMsg("删除失败！");
            }
        return result;

    }

    /**
     * 根据name获取车主信息---添加维修单弹窗
     * @param name
     * @return
     */
    @Override
    public Map findCustomerInfoByName(String name) {
        return repairDao.findCustomerInfoByName(name);
    }
    /**
     * 根据tel获取车主信息---添加维修单弹窗
     * @param tel
     * @return
     */
    @Override
    public Map findCustomerInfoByTel(String tel) {
        return repairDao.findCustomerInfoByTel(tel);
    }
    /**
     * 根据carcode获取车主信息---添加维修单弹窗
     * @param carcode
     * @return
     */
    @Override
    public Map findCustomerInfoByCarcode(String carcode) {
        return repairDao.findCustomerInfoByCarcode(carcode);
    }

    /**
     * 根据门店id 查询该店的维修人员
     * @return
     */
    @Override
    public List<BackEmployeeinfo> listEmp() {
        //获取门店id
        int schoolId= WlCommonsCode.isAdmin();

        return repairDao.listEmp(schoolId);
    }

    /**
     * 根据repairid获取该维修单的详情信息
     * @param repairid
     * @return
     */
    @Override
    public Map findRepairInfoById(String repairid) {
        HashMap map = new HashMap();
        map.put("data",repairDao.findRepairInfoById(repairid));
        map.put("code", ISystemConst.SU_COD);
        return map;
    }

    /**
     * 提交维修单
     * @param backCarRepair
     * @return
     */
    @Transactional
    @Override
    public Result addRepairInfo(BackCarRepair backCarRepair, double reprice, double coprice, double total, String[] arr) {
        //生成repairid 维修单号
        String repairid = RandomUtil.getRandom();
        //生成订单单号expenseorder
        String expenseorder = OrderNum.createOrderNumber();

        //获取当前系统时间作为创建时间createtime
        Date date = new Date(System.currentTimeMillis());

        //获取当前门店的id
        int schoolId= WlCommonsCode.isAdmin();

        backCarRepair.setRepairid(repairid);
        backCarRepair.setExpenseorder(expenseorder);
        backCarRepair.setCreatetime(date);
        backCarRepair.setSchoolId(schoolId);
        //插入订单表
        BackExpenseorder backExpenseorder = new BackExpenseorder();

        backExpenseorder.setExpenseorder(expenseorder);
        backExpenseorder.setTelephone(backCarRepair.getTelephone());
        backExpenseorder.setEmployeeid(backCarRepair.getEmployeeid());
        backExpenseorder.setCreatetime(date);
        if (reprice> ISystemConst.SU_COD){
            if (coprice> ISystemConst.SU_COD){
                //插入维修费用单  费用为reprice
                backExpenseorder.setExpensecost(reprice);
                backExpenseorder.setTotal(total);
                repairDao.addExpenseOrderRe(backExpenseorder);
                //对零件的id数组进行遍历
                for(int i=0;i<arr.length;i++){
                    //插入零件费用的单  费用为该零件对应的费用
                    backExpenseorder.setExpensecost(repairDao.findExpenseCost(Integer.valueOf(arr[i])));
                    backExpenseorder.setGoodsid(Integer.valueOf(arr[i]));
                    repairDao.addExpenseOrder(backExpenseorder);
                    //修改仓库的数量-----减1
                    repairDao.updateEntrepotInfo(Integer.valueOf(arr[i]));
                }
            }else{
                //如果只有维修费用  没有更换零件  只插入维修费用单  费用总额为reprice

                backExpenseorder.setExpensecost(reprice);
                backExpenseorder.setTotal(reprice);
                repairDao.addExpenseOrderRe(backExpenseorder);
            }
        }else{
            if (coprice> ISystemConst.SU_COD){
                //如果只是更换零件  只插入零件费用单----对零件的id数组进行遍历
                for(int i=0;i<arr.length;i++){
                    //插入零件费用的单  费用为该零件对应的费用
                    backExpenseorder.setTotal(coprice);
                    backExpenseorder.setExpensecost(repairDao.findExpenseCost(Integer.valueOf(arr[i])));
                    backExpenseorder.setGoodsid(Integer.valueOf(arr[i]));
                    repairDao.addExpenseOrder(backExpenseorder);
                    //修改仓库的数量----减1
                    repairDao.updateEntrepotInfo(Integer.valueOf(arr[i]));
                }
            }
        }
        //生成消费记录表
        BackExpenserecord backExpenserecord = new BackExpenserecord();

        backExpenserecord.setTelephone(backCarRepair.getTelephone());
        backExpenserecord.setExpenseorder(expenseorder);
        backExpenserecord.setEmployeeid(backCarRepair.getEmployeeid());
        backExpenserecord.setCreatetime(date);

        repairDao.addExpenseRecord(backExpenserecord);
        int ret = repairDao.addRepairInfo(backCarRepair);
        Result result = new Result();
            if(ret> ISystemConst.SU_COD){
                result.setMsg("添加成功！");
            }else{
                result.setMsg("添加失败！");
            }
        return result;
    }

    /**
     * 根据门店id获取该门店的零件信息
     * @return
     */
    @Override
    public List<Map> listComponent() {
        //获取门店id
        int schoolId= WlCommonsCode.isAdmin();

        return repairDao.listComponent(schoolId);
    }
}
